package com.bjsxt.rocket;

import com.alibaba.fastjson.JSON;
import com.bjsxt.model.MessagePayload;
import com.bjsxt.vo.ResponseEntity;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.tio.core.Tio;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.starter.TioWebSocketServerBootstrap;

@Component
@Slf4j
public class RocketStreamListener {

    private TioWebSocketServerBootstrap tioWebSocketServerBootstrap;

    @StreamListener("tio_group")
    public  void handleMessage(MessagePayload object){
        log.info("接收到mq的消息===》{}", JSON.toJSONString(object));

        ResponseEntity responseEntity = new ResponseEntity();


        responseEntity.setSubbed(object.getChannel());
        responseEntity.put("result",object.getBody());


        if (StringUtils.hasText(object.getUserId())){



            Tio.sendToUser(tioWebSocketServerBootstrap.getServerTioConfig(),object.getUserId(),responseEntity.build());

        return;
        }
        @NonNull String channel = object.getChannel();
        Tio.sendToGroup(tioWebSocketServerBootstrap.getServerTioConfig(),channel,responseEntity.build());
    }

}
